package com.tang.pojo;
import java.util.ArrayList;
public class Cart {
    public Cart(){
      items=new ArrayList<CartItems>();
    }
    public Cart(double priceTotal, int countTotal, ArrayList<CartItems> items) {
        this.items=new ArrayList<CartItems>();
        this.priceTotal = priceTotal;
        this.countTotal = countTotal;
        this.items = items;
    }
    public double getPriceTotal() {
        priceTotal=0;
        for(CartItems cat:items){
            priceTotal+=cat.getPriceTotal();
        }
        return priceTotal;
    }
    public int getCountTotal() {
        countTotal=0;
        for(CartItems cat:items){
            countTotal+=cat.getCount();
        }
        return countTotal;
    }
    public ArrayList<CartItems> getItems() {
        return items;
    }
    public void setItems(ArrayList<CartItems> items) {
        this.items = items;
    }
    private double priceTotal;
    private int countTotal;
    private ArrayList<CartItems> items;
    @Override
    public String toString() {
        return "Cart{" +
                "priceTotal=" + priceTotal +
                ", countTotal=" + countTotal +
                ", items=" + items +
                '}';
    }
    public void addItem(CartItems item){
        boolean flag=false;
        for(CartItems cit:items){
            if(cit.getName().equals(item.getName())){
                flag=true;
                cit.setCount(cit.getCount()+1);
            }
        }
        if(!flag){
            items.add(item);
        }
    }
    public void deleteItem(int id){
        items.remove(items.get(id));
    }
    public void updateCount(int id,int count){
        items.get(id).setCount(count);
    }
    public void clearCart(){
        items.clear();
    }
    public int queryId(String name){
        for(CartItems cit:items){
            if(cit.getName().equals(name)){
                return items.indexOf(cit);
            }
        }
        return -1;
    }
}
